//
//  PushTransitionAnimation.m
//  29-TransitionAnimation
//
//  Created by dllo on 16/8/23.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "PushTransitionAnimation.h"
#import "ViewController.h"
#import "SecondViewController.h"

@implementation PushTransitionAnimation
#pragma mark - 设置转场动画时间
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    return 0.5f;
}
#pragma mark - 执行转场动画
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    // 获取fromVC
    ViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    // 获取toVC
    SecondViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    // 获取转场View容器
    UIView *containerView = [transitionContext containerView];
    // 将toVC上的View添加到转场view上
    [containerView addSubview:toVC.view];
    // 隐藏第二个几面的view上得imageView
    toVC.toVCImageView.hidden = YES;
    // 截图的功能
    UIView *snapView = [fromVC.fromVCImageView snapshotViewAfterScreenUpdates:NO];
    // 获取到imageView的fram
    snapView.frame = fromVC.fromVCImageView.frame;
    // 将view添加到转场容器中
    [containerView addSubview:snapView];
    // 转场动画
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        // 必写的一句话
        [containerView layoutIfNeeded];
        // 给设置frame
        snapView.frame = toVC.toVCImageView.frame;
    } completion:^(BOOL finished) {
        // 显示toVC上的图片
        toVC.toVCImageView.hidden = NO;
        // 将转场上的fromVC的image移除
        [snapView removeFromSuperview];
        // 结束转场
        [transitionContext completeTransition:YES];
    }];
    
    
}

@end
